package com.pro.common.modules.service.dependencies.config.component;

import com.pro.framework.api.intf.ThirdExceptionClassifier;
import com.pro.framework.api.model.ThirdException;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ThirdExceptionDispatcher {

    private final List<ThirdExceptionClassifier> classifiers;
    private final DefaultThirdExceptionClassifier defaultClassifier;

    public ThirdExceptionDispatcher(
            List<ThirdExceptionClassifier> classifiers,
            DefaultThirdExceptionClassifier defaultClassifier) {
        this.classifiers = classifiers;
        this.defaultClassifier = defaultClassifier;
    }

    public ThirdException dispatch(String platform, Throwable e) {
        for (ThirdExceptionClassifier c : classifiers) {
            if (c.getPlatformName().equalsIgnoreCase(platform)) {
                ThirdException ex = c.classify(e);
                if (ex != null) {
                    return ex;
                }
            }
        }
        return defaultClassifier.classify(e);
    }
}
